package com.xd.core.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xd.common.exception.XkdException;
import com.xd.common.util.SecurityUtil;
import com.xd.core.system.dto.AddGroupDto;
import com.xd.core.system.dto.UpdateGroupDto;
import com.xd.core.system.entity.GroupInfo;
import com.xd.core.system.service.IGroupInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZX
 * @since 2024-07-25
 */
@Api(tags = "部门管理")
@Slf4j
@RestController
@RequestMapping("/system/group-info")
public class GroupInfoController {
    @Autowired
    private IGroupInfoService groupInfoService;


    @PreAuthorize("hasAnyAuthority('sys:DepartmentManagement-add')")
    @ApiOperation(value = "新增部门")
    @PostMapping("/addGroup")
    public void addGroup(@RequestBody AddGroupDto addGroupDto) {
        groupInfoService.addGroup(addGroupDto);
    }

    @PreAuthorize("hasAnyAuthority('sys:DepartmentManagement-Update')")
    @ApiOperation(value = "修改部门")
    @PostMapping("/updateGroup")
    public void updateGroup(@RequestBody UpdateGroupDto updateGroupDto) {
        GroupInfo groupInfo = new GroupInfo();
        BeanUtil.copyProperties(updateGroupDto, groupInfo);
        groupInfoService.updateById(groupInfo);
        log.info("修改成功");
    }


    @PreAuthorize("hasAnyAuthority('sys:DepartmentManagement')")
    @ApiOperation(value = "查询部门")
    @GetMapping("/findGroups")
    public List<GroupInfo> findGroups(String groupName) {
        return groupInfoService.findGroups(groupName);
    }
    @ApiOperation(value = "新建部门-选择上级部门下拉框")
    @GetMapping("/groupsDropDown")
    public List<GroupInfo> groupsDropDown() {
        return groupInfoService.findGroups(null);
    }


    @PreAuthorize("hasAnyAuthority('sys:DepartmentManagement-Delete')")
    @ApiOperation(value = "删除部门")
    @PostMapping("/delGroups")
    public void delGroup(Integer id) {
        groupInfoService.delGroup(id);
    }

    @ApiOperation(value = "修改员工数据权限-部门树形图")
    @GetMapping("/updateGroupsTree")
    public List<GroupInfo> updateGroupsTree(Integer userId) {
        return groupInfoService.updateGroupsTree(userId);
    }
}